{extend name="common:base" /}


{block name="breadcrumb"}
<h1>
    文章管理
    <small>文章管理</small>
</h1>
<ol class="breadcrumb">
    <li><a href="{:url('/admin')}"><i class="fa fa-dashboard"></i> 控制台</a></li>
    <li><a href="{:url('/admin/article')}">文章管理</a></li>
    <li class="active">添加文章</li>
</ol>
{/block}


{block name="content"}
<!-- Default box -->

<form action="{:url('/admin/article/add')}" method="post" style="display: block;" id="formDocument">
    <div class="row">
        <!-- left column -->
        <div class="col-md-8">
            <!-- Horizontal Form -->
            <div class="box box-info">
                <div class="box-header with-border">
                    <h3 class="box-title">基本信息</h3>
                </div>
                <!-- /.box-header -->
                <!-- form start -->
                <div class="">
                    <div class="box-body">
                        <div class="form-group">
                            <label class="control-label">标题</label>
                            <div>
                                <input type="text" name="title" class="form-control" id="inputTitle" placeholder="标题">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="control-label">内容</label>
                            <div >
                                <textarea id="editor" name="content"></textarea>
                            </div>
                        </div>


                    </div>
                    <!-- /.box-body -->

                    <!-- /.box-footer -->
                </div>
            </div>
            <!-- /.box -->
        </div>

        <!--/.col (left) -->
        <!-- right column -->
        <div class="col-md-4">
            <!-- general form elements -->
            <div class="box box-primary">
                <div class="box-header with-border">
                    <h3 class="box-title">其他信息</h3>
                </div>
                <!-- /.box-header -->
                <!-- form start -->
                <div class="box-body">
                    <div class="form-group">
                        <label class="control-label">分类</label>
                        <div>
                            <select name="category_id" class="form-control" id="inputCategoryId">
                                {foreach($cateList as $v)}
                                <option value="{$v.id}">{$v.title}</option>
                                {/foreach}
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label>封面图</label>
                        <div class="upload-box">
                            <img class="upload-box-img" src="/static/admin/dist/img/upload-pic.png">
                            <input class="upload-box-input" type="hidden" name="cover">
                            <input class="upload-input" type="file" id="file" name="file">
                        </div>
                        <p class="help-block">选择一张图片作为封面.</p>
                    </div>
                    <div class="form-group">
                        <label>启用</label>
                        <div >
                            <label><input type="checkbox" class="flat-red" checked> </label>
                        </div>
                    </div>
                </div>
                <!-- /.box-body -->
            </div>
            <!--&lt;!&ndash; /.box &ndash;&gt;-->
        </div>
        <!--/.col (right) -->
        <div class="col-sm-12 p10">
            <div class="submit-box p10">
                <button type="submit" class="btn btn-danger">提交</button>
                <button type="button" class="btn btn-default btn-cancel">取消</button>
            </div>
        </div>

    </div>
</form>
<!-- /.box -->
{/block}
{block name="js"}
<script src="/static/admin/dist/js/ajaxfileupload.js"></script>
<!-- CK Editor -->
<script src="/static/admin/bower_components/ckeditor/ckeditor.js"></script>
<script>
    CKEDITOR.replace('editor',{
        // uiColor: '#00c0ef',
        height: 200,
        // 图片上传相关
        filebrowserImageUploadUrl : './upload',// 图片上传路径
        image_previewText : ' ', // 图片信息面板预览区内容的文字内容，默认显示CKEditor自带的内容
        removeDialogTabs : 'image:advanced;image:Link', // 移除图片上传页面的'高级','链接'页签
    });
    // init validate
    $.validator.setDefaults({
        submitHandler: function(form) {
            // form.submit();
            var url = $(form).attr('action');
            var content = CKEDITOR.instances.editor.getData();
            var data = $(form).serialize();
            data += '&content='+content;
            $.ajax({
                url: url,
                type : "POST",
                data: data,
                success: function(result){
                    if (result.code === 1){
                        $.notify({
                            message: result.msg,
                            icon: 'glyphicon glyphicon-ok'
                        }, {
                            allow_dismiss: true,
                            type: 'success'
                        });
                        var tOut = setTimeout(function(){
                            clearTimeout(tOut);
                            window.location.href = result.url;
                        },1000);
                    }else{
                        $.notify({
                            message: result.msg,
                            icon: 'glyphicon glyphicon-warning-sign'
                        }, {
                            allow_dismiss: true,
                            type: 'warning'
                        });
                    }
                }

            });
        },
        errorPlacement: function(error, element) {
            $(element).parent().append(error);
        }
    });
    $("#formDocument").validate({
        rules: {
            title: 'required',
            content: 'required',
        },
        messages: {
            title: "请输入标题",
            content: "请输入文章内容",
        }
    });
    $('.upload-input').change(function () {
        var pDom = $(this).parent();
        $.ajaxFileUpload({
            url: '/admin/upload', //用于文件上传的服务器端请求地址
            secureuri: false, //是否需要安全协议，一般设置为false
            fileElementId: 'file', //文件上传域的ID
            dataType: 'json', //返回值类型 一般设置为json
            success: function (data, status) {
                var path = data.file_path;
                //console.log(_this.parent());
                pDom.find('img.upload-box-img').attr('src',path);
                pDom.find('input.upload-box-input').val(path);
            },
            error: function (data, status, e) {
                alert(e);
            }
        })
    });



</script>
{/block}